﻿using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Data.SqlClient;
public partial class UserWork_InfoPub : System.Web.UI.Page
{
    
    protected void Page_Load(object sender, EventArgs e)
    {

        if (!IsPostBack)
        {
            SqlConnection con = new SqlConnection(ConfigurationManager.AppSettings["Connection"]);
            con.Open();
            string username = Session["UserName"].ToString();
            string sql = "select * from grouping where username='" + username + "' ";
            SqlDataAdapter da = new SqlDataAdapter(sql, con);
            DataSet ds = new DataSet();
            da.Fill(ds);
            Consort.DataSource = ds;
            Consort.DataTextField = "Type";
            Consort.DataValueField = "groupingid";
            Consort.DataBind();
            con.Close();

            if (Request["Sid"] != null)
            {
                this.Submit1.Visible = false;
                this.Button1.Visible = true;
                int sid = Convert.ToInt32(Request["Sid"].ToString());
                this.Label1.Text = "修改联系人";
                SqlConnection con1 = new SqlConnection(ConfigurationManager.AppSettings["Connection"]);
                con1.Open();
                string sql1 = "select * from AddressList where sid=" + sid + "";
                SqlCommand com1 = new SqlCommand(sql1, con1);
                SqlDataReader dr1 = com1.ExecuteReader();
                if (dr1.Read())
                {


                    Consort.Items.FindByValue(dr1["Groupingid"].ToString()).Selected = true;
                        Sex.Items.FindByValue(dr1["sex"].ToString()).Selected = true;
                        Marry.Items.FindByValue(dr1["marry"].ToString()).Selected = true;
                    this.Name.ReadOnly = true;
                  
                    Name.Text = dr1["name"].ToString();
                   // Sex.Items.Clear();
                   // Marry.Items.Clear();
                   // Sex.SelectedItem.Text = dr["sex"].ToString();
                   // Sex.Items.Add(dr["sex"].ToString());
                    //Marry.Items.Add(dr["marry"].ToString());
                    //Marry.SelectedItem.Text = dr["marry"].ToString();
                    Birthday.Text = dr1["Birthday"].ToString();


                    Country.Text = dr1["Country"].ToString();
                    Province.Text = dr1["Province"].ToString();
                    City.Text = dr1["City"].ToString();
                    OrgName.Text = dr1["OrgName"].ToString();
                    OrgAddress.Text = dr1["OrgAddress"].ToString();
                    OrgPost.Text = dr1["OrgPost"].ToString();
                    OrgTel.Text = dr1["OrgTel"].ToString();
                    OrgFax.Text = dr1["OrgFax"].ToString();
                    Department.Text = dr1["Department"].ToString();
                    Duty.Text = dr1["Duty"].ToString();
                    Url.Text = dr1["Url"].ToString();
                    Email.Text = dr1["Email"].ToString();
                    HomeAddress.Text = dr1["HomeAddress"].ToString();
                    HomePost.Text = dr1["HomePost"].ToString();
                    HomeTel.Text = dr1["HomeTel"].ToString();
                    Mobile.Text = dr1["Mobile"].ToString();
                    QQ.Text = dr1["QQ"].ToString();
                    Msn.Text = dr1["Msn"].ToString();
                    Intro.Text = dr1["Intro"].ToString();

                    con1.Close();
                }
               
            }
           
        }
    }
    protected void Submit1_ServerClick(object sender, EventArgs e)
    {
        if (Name.Text.Trim() == "")
        {
            Response.Write("<script>alert('请添加联系人姓名！')</script>");
        }
        else
        {

            string names = Name.Text.Trim();
            string sexs = Sex.SelectedItem.Text;
            string marrts = Marry.SelectedItem.Text;
            string Birthdays = Birthday.Text.Trim();
            string Consorts = Consort.SelectedItem.Text;
            string Countrys = Country.Text.Trim();
            string Provinces = Province.Text.Trim();
            string Citys = City.Text.Trim();
            string OrgNames = OrgName.Text.Trim();
            string OrgAddresss = OrgAddress.Text.Trim();
            string OrgPosts = OrgPost.Text.Trim();
            string OrgTels = OrgTel.Text.Trim();
            string OrgFaxs = OrgFax.Text.Trim();
            string Departments = Department.Text.Trim();
            string Dutyss = Duty.Text.Trim();
            string Urls = Url.Text.Trim();
            string Emails = Email.Text.Trim();
            string HomeAddresss = HomeAddress.Text.Trim();
            string HomePosts = HomePost.Text.Trim();
            string HomeTels = HomeTel.Text.Trim();
            string Mobiles = Mobile.Text.Trim();
            string QQs = QQ.Text.Trim();
            string Msns = Msn.Text.Trim();
            string Intros = Intro.Text.Trim();
            string usernames = Session["UserName"].ToString();
            string PubDate = DateTime.Now.ToString();
            int Groupingid = Convert.ToInt32(Consort.SelectedItem.Value.ToString());
            SqlConnection con = new SqlConnection(ConfigurationManager.AppSettings["Connection"]);
            con.Open();
            string sql = "insert addresslist values('" + names + "','" + sexs + "','" + marrts + "','" + Birthdays + "','" + Consorts + "'," +
                "'" + Countrys + "','" + Provinces + "','" + Citys + "','" + OrgNames + "','" + OrgAddresss + "','" + OrgPosts + "','" + OrgTels + "'," +
                "'" + OrgFaxs + "','" + Departments + "','" + Dutyss + "','" + Urls + "','" + Emails + "','" + HomeAddresss + "','" + HomePosts + "'," +
                "'" + HomeTels + "','" + Mobiles + "','" + QQs + "','" + Msns + "','" + Intros + "','" + usernames + "','" + PubDate + "'," + Groupingid + ")";

            SqlCommand com = new SqlCommand(sql, con);
            com.ExecuteNonQuery();
            Response.Write("<script>alert('添加成功！！');window.location.href='Vindicate.aspx';</script>");
        }
        //string sql = "insert AddressList values('"++"',)";
    }
    protected void Button1_Click(object sender, EventArgs e)
    {
        SqlConnection con = new SqlConnection(ConfigurationManager.AppSettings["Connection"]);
        con.Open();

        int sid = Convert.ToInt32(Request["Sid"].ToString());
        string sql = "update addresslist set Marry='" + Marry.SelectedItem.Text + "',Birthday='" + Birthday.Text + "',Consort='" + Consort.SelectedItem.Text + "',Country='" + Country.Text.Trim() + "',Province='" + Province.Text + "',City='" + City.Text + "',OrgName='" + OrgName.Text + "',OrgAddress='" + OrgAddress.Text + "',OrgPost='" + OrgPost.Text + "',OrgTel='" + OrgTel.Text + "',OrgFax='" + OrgFax.Text + "',Department='" + Department.Text + "',Duty='" + Duty.Text + "',Url='" + Url.Text + "',Email='" + Email.Text + "',HomeAddress='"+HomeAddress.Text+"',"+
            "HomePost='" + HomePost.Text + "',HomeTel='" + HomeTel.Text + "',Mobile='" + Mobile.Text + "',QQ='" + QQ.Text + "',Msn='" + Msn.Text + "',Intro='" + Intro.Text + "' where sid=" + sid + " ";
        try
        {
            SqlCommand com = new SqlCommand(sql,con);
            com.ExecuteNonQuery();
            Response.Write("<script>alert('修改成功!');window.location.href='Vindicate.aspx';</script>");
        }
        catch (Exception ex)
        {
            throw (ex);
        }
    }
    protected void Button2_Click(object sender, EventArgs e)
    {
        Response.Redirect("Vindicate.aspx");
    }
}
